#ifndef GAMELOGICAL_H
#define GAMELOGICAL_H

#include "Pet.h"

#include "../_Global/GameModel.h"
#include "../_Global/GlobalClass.h"

#include <QObject>



#define MAX_PET_COUNT 20
#define MAX_MAGIC_TYPE 5



struct _CommonGameData
{
    _CommonGameData() {}

    QList<_Pet*> m_listPets;    //玩家的宠物
    //Pet Pets[MaxPets];//用户宠物数据
};



//平常状态类
class GameNormalLogical : public QObject
{
    Q_OBJECT


public:
    virtual int InitOnce()                          //进行初始化(仅1次，连同QML,由外部配置好后调用)
    {
        return 0;
    }


public:
    explicit GameNormalLogical(QObject *parent = nullptr);
    virtual ~GameNormalLogical()
    {

    }

signals:
    //void s_fightMessage(QString msg);   //战斗信息

public:

public:
    QList<_Pet*> &listPets();


public:
    _CommonGameData *m_pCommonGameData;


};







//战斗控制类
class GameFightLogical : public GameModel
{
    Q_OBJECT

public:
    //伤害
    struct _Harm
    {
        _Harm(int code = 0, long h = 0):
            nHarm(h),
            nSuccessCode(code)
        {
            memset(nMagicHarm,0,sizeof(nMagicHarm));
        }

        long nHarm;  //伤害值
        int nSuccessCode;    //是否成功.0(miss);1(无效果);2(成功);3(失败)
        int nMagicHarm[MAX_MAGIC_TYPE];   //属性伤害
    };

    struct _UserChoice
    {
        _UserChoice()
        {
            target = -1;
            choiceAttack = -1;
            choiceDefence = -1;
        }
        int target;     //目标
        int choiceAttack;   //攻击属性
        int choiceDefence;  //防御属性
    };

public:
    explicit GameFightLogical(QObject *parent = nullptr);
    virtual ~GameFightLogical()
    {

    }


signals:
    void s_fightMessage(QString msg);   //战斗信息


//框架：
public:
    virtual int InitOnce() Q_DECL_OVERRIDE          //进行初始化(仅1次，连同QML,由外部配置好后调用)
    {
        int ret = GameModel::InitOnce();
        if(ret != 0)
            return ret;

        emit s_InitOnceFinished();                  //游戏结束时通知(多线程时用线程的finished来替代!!!)
        return ret;
    }

    virtual int InitOnceInThread() Q_DECL_OVERRIDE  //线程中初始化(1次)
    {
        return GameModel::InitOnceInThread();
    }

    //每次游戏开始前初始化,sr为随机数种子
    virtual int GameInit(int sr = -1, QVariantMap param = QVariantMap()) Q_DECL_OVERRIDE
    {
        int ret = GameModel::GameInit(sr, param);

        m_listMyPets.clear();
        m_eWinner = _Pet::JiaoSe_NULL;

        m_listMyPets.append((_Pet*)param["0"].value<void*>());
        //m_listFightPets.append(m_GameNormalLogical.m_listPets.at(0));
        createEnemyPets();


        emit s_GameInitFinished();               //游戏结束时通知(多线程时用线程的finished来替代!!!)
        return ret;
    }

    //游戏开始调用
    virtual _GameStatus GameBegin() Q_DECL_OVERRIDE
    {
        //Sleep(1000);
        _GameStatus status = GameModel::GameBegin();


        emit s_GameBegin();
        return status;
    }
    //游戏结束
    virtual int GameOver() Q_DECL_OVERRIDE
    {
        _GameStatus status = gameStatus();
        int code = m_nGameStatusCode;
        int ret = GameModel::GameOver();

        emit s_GameOver(status, code);
        return ret;
    }

    virtual _GameStatus GameAdvance() Q_DECL_OVERRIDE
    {
        if(gameStatus() == Game_Status_Normal)
        {
            if(m_UserChoice.target >= 0)
                if(HuiHe(m_UserChoice) != Game_Status_Normal)
                    GameOver();
            return gameStatus();
        }
        else
            return gameStatus();

    }

    //检测游戏是否结束
    //遍历列表,我方或敌方生还者为0则结束
    virtual _GameStatus CheckGameOver() Q_DECL_OVERRIDE;


public slots:

    int createEnemyPets();

    //计算伤害
    _Harm DoHarm(_Pet *Pet1,_Pet *Pet2);

    //战斗回合
    _GameStatus HuiHe(_UserChoice &userChoice);

    bool isWin();



public:
    _CommonGameData *m_pCommonGameData;


    QList<_Pet*> m_listMyPets; //战斗时宠物数据
    QList<_Pet*> m_listFriendPets; //战斗时宠物数据
    QList<_Pet*> m_listEnemyPets;   //敌人

    _Pet::_JiaoSe m_eWinner;
    _UserChoice m_UserChoice;

};

#endif // GAMELOGICAL_H
